/**
 * @title: DynaSqlProvider.java
 * @package com.wehealth.weiyu.common.data.mybatis
 * @description: TODO
 * @author hushsh
 * @date 2016年12月30日下午4:59:00
 * @version V1.0
 */

package com.apiserver.data.mybatis.sqlProvider;


import com.apiserver.data.mybatis.model.EntityTable;
import com.apiserver.data.mybatis.util.EntityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

/**
 * @Author feizhou
 * @Description 生成mybatis动态sql
 * @Date 下午 2:33 2019/10/13 0013
 * @Param
 * @return
 **/
@Slf4j
public class UpdProvider extends Provider {


    /**
     * @return java.lang.String
     * @Author feizhou
     * @Description 通用修改语句
     * @Date 下午 10:01 2019/10/13 0013
     * @Param [m]
     **/

    public <M> String updateById(Map<String, Object> param) {
        M m = (M) param.get("m");
        EntityTable entity = EntityUtil.getEntityTable(m);

        SQL sql = new SQL().UPDATE(entity.getTableName());


        String setSql = getSetSql(m, entity);

        if (setSql == null) {
            return null;
        }

        sql.SET(setSql);

        String PKColumn = entity.getPKColumn();
        String PKProperty = entity.getPKProperty();

        sql = sql.WHERE(PKColumn + "=#{m." + PKProperty + "}");


        log.debug(">>>>sql:   ", sql.toString());
        System.out.println(">>>>sql:   " + sql.toString());
        return sql.toString();
    }

    /**
     * @return java.lang.String
     * @Author feizhou
     * @Description 通用修改语句
     * @Date 下午 10:01 2019/10/13 0013
     * @Param [m]
     **/

    public <M> String update(Map<String, Object> param) {


        M m = (M) param.get("m");


        Map<String, Object> queryMap = (Map<String, Object>) param.get("queryMap");

        EntityTable entity = EntityUtil.getEntityTable(m);


        SQL sql = new SQL().UPDATE(entity.getTableName());

        String setSql = getSetSql(m, entity);

        if (setSql == null) {
            return null;
        }

        sql.SET(setSql);

        sql = getWhere(queryMap, sql, entity);


        log.debug(">>>>sql:   ", sql.toString());
        System.out.println(">>>>sql:   " + sql.toString());
        return sql.toString();
    }

}
